package com.ruoyi.project.cms.product.controller;

import com.ruoyi.common.constant.ProductConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.product.domain.ProductSpec;
import com.ruoyi.project.cms.product.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CreateTime 2020/5/17 16:59
 *  产品规格信息
 */
@Controller
@RequestMapping("/product/spec")
public class ProductSpecController extends BaseController {
    //指定一个全局地址前缀
    private String prefix = "/cms/product/spec";

    @Autowired
    private SpecService specService;

    //页面主体跳转
//    @RequiresPermissions("system:post:view") 权限过滤后期再处理
    @GetMapping("")
    public String spec()
    {
        return prefix + "/spec";
    }

    /**
     * 主页面菜单显示
     */
//    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductSpec spec)
    {
        startPage();
        List<ProductSpec> list = specService.findAll(spec);
        return getDataTable(list);
    }

    /**
     * 增加一个产品规格
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 新增产品规格实现
     */
//    @RequiresPermissions("system:post:add")
//    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSpec(@Validated ProductSpec spec)
    {
        if (ProductConstants.PRODUCT_SPEC_NAME_NOT_UNIQUE.equals(specService.checkSpecNameUnique(spec)))
        {
            return error("新增规格'" + spec.getSpecName() + "'失败，规格名称已存在");
        }
        return toAjax(specService.addSpec(spec));
    }

    /**
     * 校验规格名称
     */
    @PostMapping("/checkSpecNameUnique")
    @ResponseBody
    public String checkSpecNameUnique(ProductSpec spec)
    {
        return specService.checkSpecNameUnique(spec);
    }


    /**
     * 根据id查询
     *    数据回显
     */
    @GetMapping("/edit/{specId}")
    public String editSpec(@PathVariable("specId") Long specId, ModelMap mmap)
    {
        mmap.put("spec",specService.findSpecById(specId));
        return prefix + "/edit";
    }
    /**
     * 修改保存岗位
     */
//    @RequiresPermissions("system:post:edit")
//    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSaveSpec(@Validated ProductSpec spec)
    {
        if (ProductConstants.PRODUCT_SPEC_NAME_NOT_UNIQUE.equals(specService.checkSpecNameUnique(spec)))
        {
            return error("修改规格'" + spec.getSpecName() + "'失败，规格名称已存在");
        }
        return toAjax(specService.changeSpec(spec));
    }

    /**
     * 查看规格的具体信息
     */
//    @RequiresPermissions("monitor:operlog:detail")
    @GetMapping("/detail/{specId}")
    public String detailSpec(@PathVariable("specId") Long specId, ModelMap mmap)
    {
        mmap.put("spec",specService.findSpecById(specId));
        return prefix + "/detail";
    }

    /**
     * 删除规格信息
     */
//    @RequiresPermissions("system:post:remove")
//    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeSpec(String ids)
    {
        try
        {
            return toAjax(specService.removeSpecByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
